package com.micro.school.order.controller;

import com.alibaba.druid.util.StringUtils;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.core.utils.poi.ExcelUtil;
import com.micro.school.common.core.web.controller.BaseController;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.core.web.page.TableDataInfo;
import com.micro.school.common.log.annotation.Log;
import com.micro.school.common.log.enums.BusinessType;
import com.micro.school.order.entity.PayStudentPrepayment;
import com.micro.school.order.service.PayStudentPrepaymentService;
import com.micro.school.system.api.domain.vo.PayStudentPrepaymentVos;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/payStudentPrepayment")
public class PayStudentPrepaymentController extends BaseController {

    @Resource
    private PayStudentPrepaymentService payStudentPrepaymentService;


    @GetMapping("/queryAllTable")
    public TableDataInfo queryAllTable(PayStudentPrepayment payStudentPrepayment){
        payStudentPrepayment.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
        payStudentPrepayment.setDeleteFlag("0");
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            payStudentPrepayment.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            payStudentPrepayment.setCollegeIds(StringUtils.isEmpty(SecurityContextHolder.getUserCollegeId()) ? null: SecurityContextHolder.getUserCollegeId());
        }
        startPage();
        List<PayStudentPrepayment> payStudentPrepaymentList = payStudentPrepaymentService.queryAllTable(payStudentPrepayment);
        return getDataTable(payStudentPrepaymentList);
    }

    @PostMapping("/insertOne")
    public AjaxResult insertOne(@RequestBody PayStudentPrepayment payStudentPrepayment){
        payStudentPrepayment.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
        payStudentPrepaymentService.insertOne(payStudentPrepayment);
        return success("添加成功");
    }

    @PostMapping("/getStudentPrepaymentVos")
    public AjaxResult getStudentPrepaymentVos(@RequestBody PayStudentPrepaymentVos payStudentPrepaymentVos){
        return success(payStudentPrepaymentService.getStudentPrepaymentVos(payStudentPrepaymentVos));
    }

    @PostMapping("/getStudentPrepaymentNoVos")
    public AjaxResult getStudentPrepaymentNoVos(@RequestBody PayStudentPrepaymentVos payStudentPrepaymentVos){
        return success(payStudentPrepaymentService.getStudentPrepaymentNoVos(payStudentPrepaymentVos));
    }

    @PostMapping("/updateCollectionStatus")
    public void updateCollectionStatus(@RequestBody PayStudentPrepaymentVos payStudentPrepaymentVos){
        payStudentPrepaymentService.updateCollectionStatus(payStudentPrepaymentVos);
    }

    @PostMapping("/updateStudentPrepayment")
    public void updateStudentPrepayment(@RequestBody PayStudentPrepaymentVos payStudentPrepaymentVos){
        payStudentPrepaymentService.updateStudentPrepayment(payStudentPrepaymentVos);
    }

    @GetMapping("/getByIdNumber")
    public AjaxResult getByIdNumber(String idNumber){
        List<PayStudentPrepayment> payStudentPrepayment = payStudentPrepaymentService.getByIdNumber(idNumber);
        return success(payStudentPrepayment);
    }

    @GetMapping("/cancel")
    public AjaxResult cancel(Integer id){
        payStudentPrepaymentService.cancel(id);
        return success("作废成功！");
    }

    @GetMapping("/delete")
    public AjaxResult delete(Integer id){
        payStudentPrepaymentService.delete(id);
        return success("删除成功！");
    }

    /**
     * 预缴费管理导出
     */
    @Log(title = "预缴费管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,PayStudentPrepayment payStudentPrepayment)
    {
        List<PayStudentPrepayment> payStudentPrepaymentList = payStudentPrepaymentService.queryAllTable(payStudentPrepayment);
        ExcelUtil<PayStudentPrepayment> util = new ExcelUtil<PayStudentPrepayment>(PayStudentPrepayment.class);
        util.exportExcel(response, payStudentPrepaymentList, "预缴费管理导出");
    }
}
